package noobteam.deartrainer.service;


import noobteam.deartrainer.mapper.UserInfoMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author 高乾威
 * 2021-6-23 19：30 用户注册功能
 */
@Service
public class UserInfoService {
    @Resource
    private UserInfoMapper userInfoMapper;

    /**
     * 监控注册功能：向用户信息表（usersinfo）插入用户名和密码
     */
    public boolean doSave(Map<String,String> map){
        boolean flag=false;
        //选择要添加监控的代码
        //ctrl+alt+t 打开 surround with窗口 选择 try catch
        try {
            boolean r=userInfoMapper.insertName(map);
            if(r){
                flag=true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            //记录日志，写到文件中进行保存
        }
        return flag;
    }

    public List<Map<String,Object>> getName(){
        List<Map<String,Object>> list = null;
        try {
            list = userInfoMapper.selectName();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    public List<Map<String,Object>> getAllInfo(){
        List<Map<String,Object>> list = null;
        try {
            list = userInfoMapper.findAll();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    public Map<String,Object> getUserInfoByName(String str){
        Map<String, Object> map = null;
        try {
            map = userInfoMapper.findUserByName(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    public Map<String,Object> getUserInfoById(int id){
        Map<String, Object> map = null;
        try {
            map = userInfoMapper.findUserById(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    public Map<String,Object> getUserPassWordByName(String str){
        Map<String, Object> map = null;
        try {
            map = userInfoMapper.selectPasswordByName(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    public boolean doUpdatePersonInfo(Map<String,String> map){
        boolean flag=false;
        try {
            boolean r = userInfoMapper.updatePersonalInfo(map);
            if(r){
                flag=true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    public boolean updatePhoto(Map<String, String> map) {
        boolean flag=false;
        try {
            boolean r = userInfoMapper.updatePhoto(map);
            if(r){
                flag=true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    public double getUserAccount(int userId) {
        double account = 0;
        try {
            account = userInfoMapper.getAccount(userId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return account;
    }

    public boolean buyCourse(Map<String, Object> map) {
        boolean isSucceed = false;
        try {
            isSucceed = userInfoMapper.buyCourse(map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return isSucceed;
    }

    public boolean addMoney(Map<String, String> map) {
        boolean isSucceed = false;
        try {
            isSucceed = userInfoMapper.addMoney(map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return isSucceed;
    }

    public boolean updatePwdByName(Map<String,String> map){
        boolean isSucceed = false;
        try {
            isSucceed = userInfoMapper.updatePassword(map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return isSucceed;
    }
}
